var express = require('express');
var router = express.Router();
const { secretKey } = require('../utils/util');
const jwt = require("jsonwebtoken");
const multiparty = require('multiparty');
const path = require('path');
const typesService = require('../service/typesService');


// 查

/**
 * @swagger
 * /types/:
 *    get:
 *      summary: 根据条件查找类型
 *      tags:
 *       - types
 *      parameters:
 *      - name: type
 *        in: query
 *        description: 搜索类型
 *        required: false
 *      - name: value
 *        in: query
 *        description: 搜索值
 *        required: false
 *      - name: current
 *        in: query
 *        description: 当前页
 *        required: false
 *      - name: pageSize
 *        in: query
 *        description: 每页显示数
 *        required: false
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */

router.get('/', async function (req, res, next) {
    let { type, value, current, pageSize } = req.query;
    let searchObj = {};
    if (type) {
        searchObj[type] = { $regex: value };
    }
    let data = await typesService.findType(searchObj, { current: current || 1, pageSize: pageSize || 5 });
    res.send(data);
});

// 查所有
/**
 * @swagger
 * /types/all:
 *    get:
 *      tags:
 *      - types
 *      summary: 获取所有类型
 *      parameters:
 *      - name: typeName
 *        in: query
 *        description: 类型
 *        required: false
 *      
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get('/all', async function (req, res) {
    let data = await typesService.findAll();
    res.send({ data });
})

// 增
/**
 * @swagger
 * /types:
 *    post:
 *      tags:
 *      - types
 *      summary: 增加类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                typeName:          
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.post('/', async function (req, res) {
    let { typeName, isShow } = req.body;
    let data = await typesService.createType({ typeName, isShow });
    console.log("增加类型：", data);
})

// 删
/**
 * @swagger
 * /types/{id}:
 *    delete:
 *      summary: 根据id删除数据
 *      tags:
 *       - types
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 类型id
 *        required: true
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */

router.delete("/:id", async function (req, res) {
    let { id } = req.params;
    let data = await typesService.removeType(id);
    console.log("删除snap");
    res.send({ data });
    console.log("删除snap,data", data);
})


// 改
/**
 * @swagger
 * /types/{id}:
 *    get:
 *      summary: 根据id查询
 *      tags:
 *       - types
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 类型id
 *        required: true
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get("/:id", async function (req, res) {
    let { id } = req.params;
    let data = await typesService.findTypeById(id);
    console.log("通过id查snap", data);
    res.send({ data });
})

// 修改
/**
 * @swagger
 * /types/{id}:
 *    put:
 *      tags:
 *      - types
 *      summary: 修改类型
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 类型id
 *        required: true
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                typeName:          
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.put('/:id', async function (req, res) {
    let { id } = req.params;
    let { typeName, isShow } = req.body;
    isShow = JSON.parse(isShow);
    let data = await typesService.updateType(id, { typeName, isShow });
    res.send({ data });
})



module.exports = router;